<?php

namespace Managers;

/**
 * Description of Acl
 *
 * @author b179a9
 */
class Acl {
    
    /**
     *
     * @var Zend_Acl 
     */
    protected $_acl = null;

    public function __construct() {
        $this->_acl = new \Zend_Acl();

        $this->addRoles();
        $this->addResources();
        $this->addPrivileges();
    }

    protected function addRoles() {

        $rolesManager = new Roles();
        $roles = $rolesManager->getTreeList();
        foreach ($roles as $role) {
            $this->_addRole($role);
        }
    }

    protected function addResources() {

        $resourcesManager = new Resources();
        $resources = $resourcesManager->getTreeList();
        foreach ($resources as $resource) {
            $this->_addResource($resource);
        }
    }

    protected function addPrivileges() {

        $rolesManager = new \Managers\Roles();
        $roles = $rolesManager->getFullList();
        foreach ($roles as $role) {
            foreach ($role['privilege'] as $privilege) {
                $this->_acl->allow($role['name'], $privilege['resource']['name'], $privilege['name']);
            }
        }
    }

    protected function _addRole($role, $parent = null) {
        $this->_acl->addRole($role['name'], $parent);
        foreach ($role['children'] as $key => $value) {
            $this->_addRole($value, $role['name']);
        }
    }

    protected function _addResource($resource, $parent = null) {
        $this->_acl->addResource($resource['name'], $parent);
        foreach ($resource['children'] as $key => $value) {
            $this->_addResource($value, $resource['name']);
        }
    }
    
    /**
     *
     * @return Zend_Acl 
     */
    public function getAcl() {
        return $this->_acl;
    }

}

?>
